import { test, expect } from 'bun:test'
/* 自行创建 */
import { scriptToken, webhookURL } from './const-value.ignore'
import { initRemoteCallWpsjsGlobal } from '../index'

test('记录 创建', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL, printLog: true })
  const infoArray = await wpsjsGlobal.Application.Record.CreateRecords({
    SheetId: 1,
    Records: [
      {
        fields: {
          '文本': '文本值',
          '关联': { recordIds: [''] },
        },
      }
    ]
  }).execution()
  const info = infoArray[0]
  const keys = ['id', 'fields']
  expect(Object.keys(info)).toEqual(expect.arrayContaining(keys))
})

test('记录 创建 全部可赋值字段', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL, printLog: true })
  const infoArray = await wpsjsGlobal.Application.Record.CreateRecords({
    SheetId: 1,
    Records: [
      {
        fields: {
          MultiLineText: 'string',
          Date: '10/1',
          Time: '5:1',
          Number: 10,
          Currency: 10,
          Percentage: 0.2,
          ID: '11010519491231002X',
          Phone: '13911010519',
          Email: '13911010519@139.com',
          Url: 'https://caisijian.cn',
          Checkbox: true,
          SingleSelect: 'a',
          MultipleSelect: ['a'],
          Rating: 1,
          Complete: 0.2,
          Contact: [
            {
              "id": "266138770",
              "nickName": "蔡",
              "avatar": "https://img.qwps.cn/266138770?imageMogr2/thumbnail/180x180!&k=1642413413010406818",
            },
          ],
          Attachment: [
            {
              "fileName": "国徽.png",
              "imgSize": "1024*1024",
              "linkUrl": "https://www.kdocs.cn/l/cfHNlyxWTbYL",
              "size": 782305,
              "source": "cloud",
              "type": "png",
              "uploadId": "cfHNlyxWTbYL"
            }
          ],
          Link: {
            recordIds: ['H', 'I']
          }
        },
      }
    ]
  }).execution()
  expect(infoArray.length).toBeGreaterThan(0)
})

test('记录 获取', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL, writeLogFile: true })
  // const info = await wpsjsGlobal.Application.Record.GetRecord({ SheetId: 1, RecordId: 'a' }).execution()
  const infoArray = await wpsjsGlobal.Application.Record.GetRecords({ SheetId: 5, }).execution()
  const keys = ['id', 'fields']
  // expect(Object.keys(info)).toEqual(expect.arrayContaining(keys))
  expect(Object.keys(infoArray.records[0])).toEqual(expect.arrayContaining(keys))
})

test('记录 更新', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL })
  const infoArray = await wpsjsGlobal.Application.Record.UpdateRecords({
    SheetId: 1,
    Records: [
      { id: 'P', fields: { '文本': '文本4' } }
    ],
  }).execution()
  const info = infoArray[0]
  const keys = ['id', 'fields']
  expect(Object.keys(info)).toEqual(expect.arrayContaining(keys))
})

test('记录 删除', async () => {
  const wpsjsGlobal = initRemoteCallWpsjsGlobal({ scriptToken, webhookURL })
  const infoArray = await wpsjsGlobal.Application.Record.DeleteRecords({ SheetId: 1, RecordIds: ['P'] }).execution()
  const info = infoArray[0]
  const keys = ['id', 'deleted',]
  expect(Object.keys(info)).toEqual(expect.arrayContaining(keys))
})
